package com.jinlejia.bbs.logic;

import android.app.Activity;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.http.entity.ContributeEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCall;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * 贡献榜Logic
 * @author 王进 on 2019/3/25.
 */

public class ContributeLogic {

    private final Activity activity;
    private final ArrayList<BBSUserBean> dataList;
    private final LogicCallBack callBack;
    public ContributeLogic(Activity activity,ArrayList<BBSUserBean> dataList,LogicCallBack callBack){
        this.activity = activity;
        this.dataList = dataList;
        this.callBack = callBack;
    }

    public void execute(){
        listRencountByMonth();
    }

    private void listRencountByMonth(){
        httpCallForm(BBSConfig.listRencountByMonth,new JSONObject())
                .execute(httpCallBack(activity, new HttpCallBack() {
                    @Override
                    public void onNoResponse(int code) {
                        if (code==204){
                            dataList.clear();
                        }
                        callBack.onResult();
                    }
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult();
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        ArrayList<BBSUserBean> datas = new ArrayList<>();
                        List<ContributeEntity> list = parse.parseArray(ContributeEntity.class);
                        for (ContributeEntity entity:list) {
                            BBSUserBean bean = new BBSUserBean();
                            bean.setUserPin(entity.getPin());
                            bean.setUserAddress(entity.getStoreName());
                            bean.setUserName(entity.getUserName());
                            bean.setAnswerCount(entity.getCount());
                            bean.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl()));
                            datas.add(bean);
                        }
                        dataList.clear();
                        dataList.addAll(datas);
                        callBack.onResult();
                    }
                }));
    }







    public interface LogicCallBack{
        void onResult();
    }
}
